import { getMenus} from "@/api/menu";

//存储要添加的路由
let menus =[]
//通过axios请求获取后端菜单数据
 await getMenus().then(res=>{
    // console.log("菜单数据:",res)
    //路由拼接
    routesData(res.data.result)
    console.log('完成拼接之后的menus:',menus)
}).catch(err=>{
    console.log("加载数据失败:",err)
})
    
function  routesData(result){ 
  menus =  result.map((item,index) =>{
         index++
         //父路由
         let menu = {  
            path: item.path,  
            name: item.name,  
            meta: { title: item.title, icon: item.icon, top: item.top }  
        }
        //子路由
        if(item.childMenus){
            menu.children = item.childMenus.map(childMenu => ({  
                path: childMenu.path,  
                name: childMenu.name,  
                meta: { title: childMenu.title, icon: childMenu.icon },  
                // component: () =>import(`../views/folder-${index}/${childMenu.component}.vue`)  
                component: () =>import(`../views/${item.name}/${childMenu.component}.vue`)  
            }))
         
        }else{
             menu.component=()=>import(`../views/${item.name}/${item.component}.vue`)
        }
        return menu
  })
  return menus        
}

export default menus